package org.ziwei.paipan.biao;

import org.ziwei.paipan.var.DiZhi;
import org.ziwei.paipan.var.TianGan;
import org.ziwei.paipan.var.YinYang;

/**
 * 基础算法工具集
 */
public class Utils {
	
	private static int[] shiChenStartHour = {23,1,3,5,7,9,11,13,15,17,19,21};
	
	private static int[] shiChenEndHour   = {1,3,5,7,9,11,13,15,17,19,21,23};
	
	/**
	 * 根据出生小时，返回对应的时辰
	 * @param hour
	 * @return DiZhi
	 */
	public static DiZhi convertToShiChen(int hour){			
		if(hour==23 || hour == 24 || hour == 0)
			return DiZhi.ZI;
		for(int i=1; i<shiChenStartHour.length; i++){
			if( hour >= shiChenStartHour[i] && hour < shiChenEndHour[i]){
				return DiZhi.values()[i];
			}
		}
		return null;
	}
	
	/**
	 * 根据年，返回该年的天干
	 * @param year
	 * @return TianGan
	 */
	public static TianGan convertToTianGan(int year){
		return TianGan.values()[(year-4)%60%10];
	}

	/**
	 * 根据年，返回该年的地支
	 * @param year
	 * @return DiZhi
	 */
	public static DiZhi convertToDiZhi(int year){
		return DiZhi.values()[(year-4)%60%12];
	}
	
	/**
	 * 根据年，返回该年是阴年还是阳年
	 * @param year
	 * @return YinYang
	 */
	public static YinYang convertToYinYang(int year){
		return YinYang.values()[(year-4)%60%2];
	}
	
	/**
	 * 人盘中活跃的星曜都是以天干定宫，文昌文曲本来以地支定宫，但在人盘中，改用天干来布宫
	 * 流昌流曲的计算方法和其他流曜不同
	 * 流昌起巳位 甲乙顺流去 不用四墓宫 日月同年岁
	 * 流曲起酉位 甲乙逆行踪 亦不用四墓 年日月相同
	 */
	private static DiZhi[] liuChangBiao = {DiZhi.SI,DiZhi.WU,DiZhi.SHEN,DiZhi.YOU,DiZhi.SHEN,DiZhi.YOU,DiZhi.HAI,DiZhi.ZI,DiZhi.YIN,DiZhi.MAO};
	private static DiZhi[] liuQuBiao = {DiZhi.YOU,DiZhi.SHEN,DiZhi.WU,DiZhi.SI,DiZhi.WU,DiZhi.SI,DiZhi.MAO,DiZhi.YIN,DiZhi.ZI,DiZhi.HAI};
	
	/**
	 * 根据大限年所在宫的宫干，返回运昌所在宫支
	 * @param tianGan 大限年所在宫干
	 * @return DiZhi
	 */
	public static DiZhi anLiuChang(TianGan tianGan){
		return liuChangBiao[tianGan.ordinal()];
	}
	
	/**
	 * 根据大限年所在宫的宫干，返回运曲所在宫支
	 * @param tianGan 大限年所在宫干
	 * @return DiZhi
	 */
	public static DiZhi anLiuQu(TianGan tianGan){
		return liuQuBiao[tianGan.ordinal()];
	}
}
